import {
    getCurrentUserPagePermissionsApi,
    getCurrentUserPagePermissionsFail,
    getCurrentUserPagePermissionsSuccess
} from 'Shared/actions/currentUserContext';
import {PAGE_CODE} from './constants';
import * as api from './api';
import {uuid} from '../utils';
import {claimUsedPartDealCategory} from '../Enum';
export const GET_BRANDS_BEGIN = 'GET_BRANDS_BEGIN';
export const GET_BRANDS_SUCCESS = 'GET_BRANDS_SUCCESS';
export const GET_BRANDS_FAIL = 'GET_BRANDS_FAIL';
export const getBrandBegin = () => ({
    type: GET_BRANDS_BEGIN
});
export const getBrandSuccess = data => ({
    type: GET_BRANDS_SUCCESS,
    data
});
export const getBrandFail = () => ({
    type: GET_BRANDS_FAIL,
});

export const getBrands = condition => dispatch => {
    dispatch(getBrandBegin());
    return api.getBrands(condition).then(res => {
        if(res.ok)
            dispatch(getBrandSuccess(res.data));
        else
            dispatch(getBrandFail());
        return res.ok;
    });
};

/*==============================初始化车型================================= */
export const GET_INIT_CATEGORY_DATA_BEGIN = 'GET_INIT_CATEGORY_DATA_BEGIN';
export const GET_INIT_CATEGORY_DATA_SUCCESS = 'GET_INIT_CATEGORY_DATA_SUCCESS';
export const GET_INIT_CATEGORY_DATA_FAIL = 'GET_INIT_CATEGORY_DATA_FAIL';
const getInitCategoryDataBegin = () => ({
    type: GET_INIT_CATEGORY_DATA_BEGIN
});

const getInitCategoryDataSuccess = data => ({
    type: GET_INIT_CATEGORY_DATA_SUCCESS,
    data
});
const getInitCategoryDataFail = () => ({
    type: GET_INIT_CATEGORY_DATA_FAIL
});
/*==============================初始化API================================= */
export const GET_INIT_BEGIN = 'GET_INIT_BEGIN';
export const GET_INIT_SUCCESS = 'GET_INIT_SUCCESS';
export const GET_INIT_FAIL = 'GET_INIT_FAIL';
export const getInitDataBegin = () => ({
    type: GET_INIT_BEGIN
});
export const getInitDataSuccess = data => ({
    type: GET_INIT_SUCCESS,
    data,
});
export const getInitDataFail = () => ({
    type: GET_INIT_FAIL,
});
export const getInitData = (code = PAGE_CODE) => dispatch => {
    dispatch(getInitCategoryDataBegin());
    const category = api.productCategoryQuery().then(res => {
        if(res.ok)
            dispatch(getInitCategoryDataSuccess(res.data));
        else
            dispatch(getInitCategoryDataFail());
        return res.ok;
    });
    const brands = dispatch(getBrands());
    const permissions = getCurrentUserPagePermissionsApi(PAGE_CODE).then(res => {
        if(res.isOk) dispatch(getCurrentUserPagePermissionsSuccess(res.data));
        else dispatch(getCurrentUserPagePermissionsFail(res.statusCode, res.message));
        return res.isOk;
    });
    return Promise.all([category, permissions, brands]).then(values => values[0] && values[1] && values[2]);
};
/*==============================查询列表================================= */

export const GET_LIST_BEGIN = 'GET_LIST_BEGIN';
export const GET_LIST_SUCCESS = 'GET_LIST_SUCCESS';
export const GET_LIST_FAIL = 'GET_LIST_FAIL';
export const getListBegin = () => ({
    type: GET_LIST_BEGIN
});
export const getListSuccess = (data, condition) => ({
    type: GET_LIST_SUCCESS,
    data,
    condition
});
export const getListFail = () => ({
    type: GET_LIST_FAIL,
}); //获取列表

export const getList = condition => dispatch => {
    const {dealerCode, dealerName, ...props} = condition;
    dispatch(getListBegin());
    return api.getList(props).then(res => {
        if(res.ok)
            dispatch(getListSuccess(res.data, condition));
        else
            dispatch(getListFail());
        return res.ok;
    });
}; //按钮搜索

export const querySearch = () => (dispatch, getState) => {
    const condition = getState()
        .getIn(['page', 'appState', 'queryCondition'])
        .toJS();
    dispatch(getList(condition));
}; //分页排序搜索

export const tableSearch = page => (dispatch, getState) => {
    const query = getState()
        .getIn(['page', 'appState', 'queryBySort'])
        .toJS();
    const condition = Object.assign({}, query, page);
    dispatch(getList(condition));
};

/*==============================查询action================================= */
//修改查询条件

export const SAVE_QUERY_CONDITION = 'SAVE_QUERY_CONDITION';
export const saveQueryCondition = (name, value) => ({
    type: SAVE_QUERY_CONDITION,
    name,
    value
});
//重置查询条件

export const RESET_QUERY_PANEL = 'RESET_QUERY_PANEL';
export const resetQueryPanel = () => ({
    type: RESET_QUERY_PANEL,
});


/* =======================获取万能工时================================== */
export const GET_WORKITEM_BEGIN = 'GET_WORKITEM_BEGIN';
export const GET_WORKITEM_SUCCESS = 'GET_WORKITEM_SUCCESS';
export const GET_WORKITEM_FAIL = 'GET_WORKITEM_FAIL';
export const getWorkItemBegin = () => ({
    type: GET_WORKITEM_BEGIN
});
export const getWorkItemSuccess = data => ({
    type: GET_WORKITEM_SUCCESS,
    data
});
export const getWorkItemFail = () => ({
    type: GET_WORKITEM_FAIL,
});

export const getWorkItems = (productCategoryCode, brandId) => dispatch => {
    dispatch(getWorkItemBegin());
    return api.getWorkItems({
        productCategoryCode,
        brandId
    }).then(res => {
        if(res.ok)
            dispatch(getWorkItemSuccess(res.data.content));
        else
            dispatch(getWorkItemFail());
        return res.ok;
    });
};
/* =======================获取详情================================== */
export const GET_DETAIL_BEGIN = 'GET_DETAIL_BEGIN';
export const GET_DETAIL_SUCCESS = 'GET_DETAIL_SUCCESS';
export const GET_DETAIL_FAIL = 'GET_DETAIL_FAIL';
export const getDetailBegin = () => ({
    type: GET_DETAIL_BEGIN
});
export const getDetailSuccess = data => ({
    type: GET_DETAIL_SUCCESS,
    data
});
export const getDetailFail = () => ({
    type: GET_DETAIL_FAIL,
});

export const getDetail = (id, isUpdate) => dispatch => {
    dispatch(getDetailBegin());
    return api.getDetail(id).then(res => {
        if(res.ok) {
            dispatch(getDetailSuccess(res.data));
            if(isUpdate)
                dispatch(getWorkItems(res.data.productCategoryCode, res.data.brandId));
        } else
            dispatch(getDetailFail());
        return res.ok;
    });
};
/*==============================清除数据================================= */
export const CLEAR_DATA = 'CLEAR_DATA';
export const clearData = () => ({
    type: CLEAR_DATA
});

/*==========================修改新增编辑页数据================================= */
export const SAVE_SUBMIT_CONDITION = 'SAVE_SUBMIT_CONDITION';
export const saveSubmitCondition = (name, value) => ({
    type: SAVE_SUBMIT_CONDITION,
    name,
    value
});

export const onSubmit = id => (dispatch, getState) => {
    const {
        rowVersion,
        supplierId,
        dutyUnitId,
        details = [],
        accessoryDetails = [],
        workItemDetails = []
    } = getState()
        .getIn(['page', 'appState', 'submitData'])
        .toJS();
    const detailsData = details.map(item => ({
        usedPartId: item.usedPartId,
        isCaused: item.isCaused,
        quantity: item.quantity
    }));
    const accessoryData = accessoryDetails.map(item => ({
        usedPartId: item.usedPartId,
        quantity: item.quantity
    }));
    const workItemData = workItemDetails.map(item => ({
        usedPartId: item.usedPartId,
        workItemId: item.workItemId,
        isDefault: item.isDefault,
        laborHour: item.laborHour,
        laborPrice: item.laborPrice,
    }));
    return api.updateInfo(id, {
        rowVersion,
        supplierId,
        dutyUnitId,
        details: detailsData,
        accessoryDetails: accessoryData,
        workItemDetails: workItemData
    });
};

export const SELECT_DATA = 'SELECT_DATA';
export const selectData = data => ({
    type: SELECT_DATA,
    data
});

export const selectSupplier = data => dispatch => {
    const condition = {
        supplierId: data.id,
        supplierCode: data.code,
        supplierName: data.name,
    };
    dispatch(selectData(condition));
};
export const selectDutyUnit = data => dispatch => {
    const condition = {
        dutyUnitId: data.id,
        dutyUnitCode: data.code,
        dutyUnitName: data.name,
    };
    dispatch(selectData(condition));
};


export const DELETE_DETAIL = 'DELETE_DETAIL';
export const deleteDetail = (id, path) => ({
    type: DELETE_DETAIL,
    id,
    path
});

export const DELETE_DETAIL_WORKITEM = 'DELETE_DETAIL_WORKITEM';
export const deleteDetailWorkItem = (id, path) => ({
    type: DELETE_DETAIL_WORKITEM,
    id,
    path
});
export const deleteWorkItem = id => dispatch => dispatch(deleteDetailWorkItem(id, 'workItemDetails'));
export const deletePart = id => dispatch => {
    dispatch(deleteDetail(id, 'details'));
    dispatch(deleteDetailWorkItem(id, 'workItemDetails'));
};
export const deleteAccessoryPart = id => dispatch => dispatch(deleteDetail(id, 'accessoryDetails'));


export const DELETE_ALL_DETAIL = 'DELETE_ALL_DETAIL';
export const deleteAllDetail = path => ({
    type: DELETE_ALL_DETAIL,
    path
});
export const deleteAllPart = () => dispatch => {
    dispatch(deleteAllDetail('details'));
    dispatch(deleteAllDetail('workItemDetails'));
};
export const deleteAllAccessoryPart = () => dispatch => dispatch(deleteAllDetail('accessoryDetails'));
export const deleteAllWorkItem = () => dispatch => dispatch(deleteAllDetail('workItemDetails'));

export const UPDATE_DETAIL = 'UPDATE_DETAIL';
export const updateDetails = (data, path) => ({
    type: UPDATE_DETAIL,
    data,
    path
});

export const updatePart = data => dispatch => dispatch(updateDetails(data, 'details'));
export const updateAccessoryPart = data => dispatch => dispatch(updateDetails(data, 'accessoryDetails'));

export const UPDATE_DETAIL_WORKITEM = 'UPDATE_DETAIL_WORKITEM';
export const updateWorkItemDetails = (data, path) => ({
    type: UPDATE_DETAIL_WORKITEM,
    data,
    path
});
export const updateWorkItemlaborHour = data => (dispatch, getState) => {
    const {workItemDetails = []} = getState().getIn(['page', 'appState', 'submitData']).toJS();
    const workItemDetail = workItemDetails.find(v => v.id === data.id);
    return dispatch(updateWorkItemDetails({
        id: data.id,
        laborHour: data.laborHour,
        laborFee: data.laborHour / 100 * workItemDetail.laborPrice
    }, 'workItemDetails'));
};
export const updateWorkItemlaborPrice = data => (dispatch, getState) => {
    const {workItemDetails = []} = getState().getIn(['page', 'appState', 'submitData']).toJS();
    const workItemDetail = workItemDetails.find(v => v.id === data.id);
    return dispatch(updateWorkItemDetails({
        id: data.id,
        laborPrice: data.laborPrice,
        laborFee: workItemDetail.laborHour / 100 * data.laborPrice
    }, 'workItemDetails'));
};

export const SELECT_DETAILS = 'SELECT_DETAILS';
export const selectDetails = (data, path) => ({
    type: SELECT_DETAILS,
    data,
    path
});

export const selectPart = data => dispatch => {
    const parts = data.map(item => ({
        usedPartId: item.id,
        usedPartCode: item.code,
        usedPartName: item.name,
        unit: item.unit,
        isCaused: false,
        quantity: 0,
        price: item.price,
        isDecimalAllowed: item.isDecimalAllowed,
    }));
    return dispatch(selectDetails(parts, 'details'));
};


export const selectAccessoryPart = data => dispatch => {
    const parts = data.map(item => ({
        usedPartId: item.id,
        usedPartCode: item.code,
        usedPartName: item.name,
        unit: item.unit,
        quantity: 0,
        price: item.price,
        isDecimalAllowed: item.isDecimalAllowed,
        accessorySource: item.partAssistPartSource
    }));
    return dispatch(selectDetails(parts, 'accessoryDetails'));
};

export const selectWorkItem = records => (dispatch, getState) => {
    const {workItemDetails = []} = getState().getIn(['page', 'appState', 'submitData']).toJS();
    const workItem = getState().getIn(['page', 'domainData', 'workItem', 'data']).toJS();
    const newWorkItemDetails = [];
    if(records.some(v => v.workItems.length === 0))
        records.forEach(r => {
            if(r.workItems.length < 1 && workItem)
                newWorkItemDetails.push({
                    id: uuid(),
                    usedPartId: r.id,
                    usedPartCode: r.code,
                    workItemId: workItem.id,
                    workItemCode: workItem.code,
                    workItemName: workItem.name,
                    laborHour: workItem.laborHour || 0,
                    laborPrice: workItem.laborPrice || 0,
                    laborFee: workItem.laborHour / 100 * workItem.laborPrice,
                    remark: workItem.remark,
                    isDefault: true,
                    productCategoryCode: workItem.productCategoryCode,
                    partProductCategoryCodes: r.productCategoryCode,
                    isCustomed: true
                });
            else
                r.workItems.forEach(v => {
                    const index = workItemDetails.findIndex(o => o.workItemId === v.id);
                    if(index === -1) {
                        newWorkItemDetails.push({
                            id: uuid(),
                            usedPartId: r.id,
                            usedPartCode: r.code,
                            workItemId: v.id,
                            workItemCode: v.code,
                            workItemName: v.name,
                            laborHour: v.laborHour || 0,
                            laborPrice: v.laborPrice || 0,
                            laborFee: v.laborHour / 100 * v.laborPrice,
                            remark: v.remark,
                            productCategoryCode: v.productCategoryCode,
                            partProductCategoryCodes: r.productCategoryCode
                        });
                        v.assistWorkItems.forEach(o => {
                            const index = workItemDetails.findIndex(f => f.workItemId === o.id);
                            if(index === -1)
                                newWorkItemDetails.push({
                                    id: uuid(),
                                    usedPartId: r.id,
                                    usedPartCode: r.code,
                                    workItemId: o.id,
                                    workItemCode: o.code,
                                    workItemName: o.name,
                                    laborHour: o.laborHour,
                                    laborPrice: o.laborPrice,
                                    laborFee: o.laborHour / 100 * o.laborPrice,
                                    remark: o.remark,
                                    productCategoryCode: o.productCategoryCode,
                                    partProductCategoryCodes: r.productCategoryCode
                                });
                        });
                    }
                });
        });
    else
        records.forEach(r => {
            r.workItems.forEach(v => {
                const index = workItemDetails.findIndex(o => o.workItemId === v.id);
                if(index === -1) {
                    newWorkItemDetails.push({
                        id: uuid(),
                        usedPartId: r.id,
                        usedPartCode: r.code,
                        workItemId: v.id,
                        workItemCode: v.code,
                        workItemName: v.name,
                        laborHour: v.laborHour || 0,
                        laborPrice: v.laborPrice || 0,
                        laborFee: v.laborHour / 100 * v.laborPrice,
                        remark: v.remark,
                        productCategoryCode: v.productCategoryCode,
                        partProductCategoryCodes: r.productCategoryCode
                    });
                    v.assistWorkItems.forEach(o => {
                        const index = workItemDetails.findIndex(f => f.workItemId === o.id);
                        if(index === -1)
                            newWorkItemDetails.push({
                                id: uuid(),
                                usedPartId: r.id,
                                usedPartCode: r.code,
                                workItemId: o.id,
                                workItemCode: o.code,
                                workItemName: o.name,
                                laborHour: o.laborHour || 0,
                                laborPrice: o.laborPrice || 0,
                                laborFee: o.laborHour / 100 * o.laborPrice,
                                remark: o.remark,
                                productCategoryCode: o.productCategoryCode,
                                partProductCategoryCodes: r.productCategoryCode
                            });
                    });
                }
            });
        });
    return dispatch(selectDetails(newWorkItemDetails, 'workItemDetails'));
};

export const onSelectWorkItemQuery = records => (dispatch, getState) => {
    const {workItemDetails = [], details = []} = getState().getIn(['page', 'appState', 'submitData']).toJS();
    const claimMaterialDetail = details.find(v => v.isCaused === true);
    const newWorkItemDetails = [];
    records.forEach(r => {
        const index = workItemDetails.findIndex(o => o.workItemId === r.id);
        if(index === -1)
            newWorkItemDetails.push({
                id: uuid(),
                usedPartId: claimMaterialDetail.usedPartId,
                usedPartCode: claimMaterialDetail.usedPartCode,
                workItemId: r.id,
                workItemCode: r.code,
                workItemName: r.name,
                laborHour: r.laborHour || 0,
                laborPrice: r.laborPrice || 0,
                laborFee: r.laborHour * r.laborPrice,
                remark: r.remark,
                productCategoryCode: r.productCategoryCode,
                partProductCategoryCodes: claimMaterialDetail.productCategoryCode,
                isCustomed: true
            });
    });
    return dispatch(selectDetails(newWorkItemDetails, 'workItemDetails'));
};


export const SELECT_DEALER = 'SELECT_DEALER';
export const selectDealer = data => ({
    type: SELECT_DEALER,
    data: {
        dealerId: data.id,
        dealerCode: data.code,
        dealerName: data.name,
    }
});

export const CHANGE_CAUSE = 'CHANGE_CAUSE';
export const changeCause = (id, value) => ({
    type: CHANGE_CAUSE,
    id,
    value
});

/* =======================获取批量修改详情================================== */
export const GET_MOREDETAIL_BEGIN = 'GET_MOREDETAIL_BEGIN';
export const GET_MOREDETAIL_SUCCESS = 'GET_MOREDETAIL_SUCCESS';
export const GET_MOREDETAIL_FAIL = 'GET_MOREDETAIL_FAIL';
export const getMoreDetailBegin = () => ({
    type: GET_MOREDETAIL_BEGIN
});
export const getMoreDetailSuccess = data => ({
    type: GET_MOREDETAIL_SUCCESS,
    data
});
export const getMoreDetailFail = () => ({
    type: GET_MOREDETAIL_FAIL,
});

export const getMoreDetail = (condition, productCategoryCode, brandId) => dispatch => {
    dispatch(getMoreDetailBegin());
    return api.getUpdateDetailList(condition).then(res => {
        if(res.ok) {
            dispatch(getMoreDetailSuccess(res.data));
            dispatch(getWorkItems(productCategoryCode, brandId));
        } else
            dispatch(getMoreDetailFail());
        return res.ok;
    });
};

export const SAVE_SUBMIT_CONDITION_MORE = 'SAVE_SUBMIT_CONDITION_MORE';
export const saveSubmitConditionMore = (name, value) => ({
    type: SAVE_SUBMIT_CONDITION_MORE,
    name,
    value
});

export const SELECT_DATA_MORE = 'SELECT_DATA_MORE';
export const selectDataMore = data => ({
    type: SELECT_DATA_MORE,
    data
});

export const selectSupplierMore = data => dispatch => {
    const condition = {
        supplierId: data.id,
        supplierCode: data.code,
        supplierName: data.name,
    };
    dispatch(selectDataMore(condition));
};
export const selectDutyUnitMore = data => dispatch => {
    const condition = {
        dutyUnitId: data.id,
        dutyUnitCode: data.code,
        dutyUnitName: data.name,
    };
    dispatch(selectDataMore(condition));
};


export const DELETE_DETAIL_MORE = 'DELETE_DETAIL_MORE';
export const deleteDetailMore = (id, path) => ({
    type: DELETE_DETAIL_MORE,
    id,
    path
});

export const DELETE_DETAIL_WORKITEM_MORE = 'DELETE_DETAIL_WORKITEM_MORE';
export const deleteDetailWorkItemMore = (id, path) => ({
    type: DELETE_DETAIL_WORKITEM_MORE,
    id,
    path
});
export const deleteWorkItemMore = id => dispatch => dispatch(deleteDetailWorkItemMore(id, 'workItemDetails'));

export const deletePartMore = id => dispatch => {
    dispatch(deleteDetailMore(id, 'details'));
    dispatch(deleteDetailWorkItem(id, 'workItemDetails'));
};
export const deleteAccessoryPartMore = id => dispatch => dispatch(deleteDetailMore(id, 'accessoryDetails'));

export const DELETE_ALL_DETAIL_MORE = 'DELETE_ALL_DETAIL_MORE';
export const deleteAllDetailMore = path => ({
    type: DELETE_ALL_DETAIL_MORE,
    path
});

export const deleteAllPartMore = () => dispatch => {
    dispatch(deleteAllDetailMore('details'));
    dispatch(deleteAllDetailMore('workItemDetails'));
};
export const deleteAllAccessoryPartMore = () => dispatch => dispatch(deleteAllDetailMore('accessoryDetails'));
export const deleteAllWorkItemMore = () => dispatch => dispatch(deleteAllDetailMore('workItemDetails'));

export const UPDATE_DETAIL_MORE = 'UPDATE_DETAIL_MORE';
export const updateDetailsMore = (data, path) => ({
    type: UPDATE_DETAIL_MORE,
    data,
    path
});

export const updatePartMore = data => dispatch => dispatch(updateDetailsMore(data, 'details'));
export const updateAccessoryPartMore = data => dispatch => dispatch(updateDetailsMore(data, 'accessoryDetails'));

export const UPDATE_DETAIL_WORKITEM_MORE = 'UPDATE_DETAIL_WORKITEM_MORE';
export const updateWorkItemDetailsMore = (data, path) => ({
    type: UPDATE_DETAIL_WORKITEM_MORE,
    data,
    path
});
export const updateWorkItemlaborHourMore = data => (dispatch, getState) => {
    const {workItemDetails = []} = getState().getIn(['page', 'appState', 'submitMoreData']).toJS();
    const workItemDetail = workItemDetails.find(v => v.id === data.id);
    return dispatch(updateWorkItemDetailsMore({
        id: data.id,
        laborHour: data.laborHour,
        laborFee: data.laborHour / 100 * workItemDetail.laborPrice
    }, 'workItemDetails'));
};
export const updateWorkItemlaborPriceMore = data => (dispatch, getState) => {
    const {workItemDetails = []} = getState().getIn(['page', 'appState', 'submitMoreData']).toJS();
    const workItemDetail = workItemDetails.find(v => v.id === data.id);
    return dispatch(updateWorkItemDetailsMore({
        id: data.id,
        laborPrice: data.laborPrice,
        laborFee: workItemDetail.laborHour / 100 * data.laborPrice
    }, 'workItemDetails'));
};


export const SELECT_DETAILS_MORE = 'SELECT_DETAILS_MORE';
export const selectDetailsMore = (data, path) => ({
    type: SELECT_DETAILS_MORE,
    data,
    path
});

export const selectPartMore = data => dispatch => {
    const parts = data.map(item => ({
        usedPartId: item.id,
        usedPartCode: item.code,
        usedPartName: item.name,
        unit: item.unit,
        isCaused: false,
        quantity: 0,
        price: item.price,
        isDecimalAllowed: item.isDecimalAllowed,
    }));
    return dispatch(selectDetailsMore(parts, 'details'));
};


export const selectAccessoryPartMore = data => dispatch => {
    const parts = data.map(item => ({
        usedPartId: item.id,
        usedPartCode: item.code,
        usedPartName: item.name,
        unit: item.unit,
        quantity: 0,
        price: item.price,
        isDecimalAllowed: item.isDecimalAllowed,
        accessorySource: item.partAssistPartSource
    }));
    return dispatch(selectDetailsMore(parts, 'accessoryDetails'));
};

export const selectWorkItemMore = records => (dispatch, getState) => {
    const {workItemDetails = []} = getState().getIn(['page', 'appState', 'submitMoreData']).toJS();
    const workItem = getState().getIn(['page', 'domainData', 'workItem', 'data']).toJS();
    const newWorkItemDetails = [];
    if(records.some(v => v.workItems.length === 0))
        records.forEach(r => {
            if(r.workItems.length < 1 && workItem)
                newWorkItemDetails.push({
                    id: uuid(),
                    usedPartId: r.id,
                    usedPartCode: r.code,
                    workItemId: workItem.id,
                    workItemCode: workItem.code,
                    workItemName: workItem.name,
                    laborHour: workItem.laborHour || 0,
                    laborPrice: workItem.laborPrice || 0,
                    laborFee: workItem.laborHour / 100 * workItem.laborPrice,
                    remark: workItem.remark,
                    isDefault: true,
                    productCategoryCode: workItem.productCategoryCode,
                    partProductCategoryCodes: r.productCategoryCode,
                    isCustomed: true
                });
            else
                r.workItems.forEach(v => {
                    const index = workItemDetails.findIndex(o => o.workItemId === v.id);
                    if(index === -1) {
                        newWorkItemDetails.push({
                            id: uuid(),
                            usedPartId: r.id,
                            usedPartCode: r.code,
                            workItemId: v.id,
                            workItemCode: v.code,
                            workItemName: v.name,
                            laborHour: v.laborHour || 0,
                            laborPrice: v.laborPrice || 0,
                            laborFee: v.laborHour / 100 * v.laborPrice,
                            remark: v.remark,
                            productCategoryCode: v.productCategoryCode,
                            partProductCategoryCodes: r.productCategoryCode
                        });
                        v.assistWorkItems.forEach(o => {
                            const index = workItemDetails.findIndex(f => f.workItemId === o.id);
                            if(index === -1)
                                newWorkItemDetails.push({
                                    id: uuid(),
                                    usedPartId: r.id,
                                    usedPartCode: r.code,
                                    workItemId: o.id,
                                    workItemCode: o.code,
                                    workItemName: o.name,
                                    laborHour: o.laborHour,
                                    laborPrice: o.laborPrice,
                                    laborFee: o.laborHour / 100 * o.laborPrice,
                                    remark: o.remark,
                                    productCategoryCode: o.productCategoryCode,
                                    partProductCategoryCodes: r.productCategoryCode
                                });
                        });
                    }
                });
        });
    else
        records.forEach(r => {
            r.workItems.forEach(v => {
                const index = workItemDetails.findIndex(o => o.workItemId === v.id);
                if(index === -1) {
                    newWorkItemDetails.push({
                        id: uuid(),
                        usedPartId: r.id,
                        usedPartCode: r.code,
                        workItemId: v.id,
                        workItemCode: v.code,
                        workItemName: v.name,
                        laborHour: v.laborHour || 0,
                        laborPrice: v.laborPrice || 0,
                        laborFee: v.laborHour / 100 * v.laborPrice,
                        remark: v.remark,
                        productCategoryCode: v.productCategoryCode,
                        partProductCategoryCodes: r.productCategoryCode
                    });
                    v.assistWorkItems.forEach(o => {
                        const index = workItemDetails.findIndex(f => f.workItemId === o.id);
                        if(index === -1)
                            newWorkItemDetails.push({
                                id: uuid(),
                                usedPartId: r.id,
                                usedPartCode: r.code,
                                workItemId: o.id,
                                workItemCode: o.code,
                                workItemName: o.name,
                                laborHour: o.laborHour || 0,
                                laborPrice: o.laborPrice || 0,
                                laborFee: o.laborHour / 100 * o.laborPrice,
                                remark: o.remark,
                                productCategoryCode: o.productCategoryCode,
                                partProductCategoryCodes: r.productCategoryCode
                            });
                    });
                }
            });
        });
    return dispatch(selectDetailsMore(newWorkItemDetails, 'workItemDetails'));
};
export const onSelectWorkItemQueryMore = records => (dispatch, getState) => {
    const {workItemDetails = [], details = []} = getState().getIn(['page', 'appState', 'submitMoreData']).toJS();
    const claimMaterialDetail = details.find(v => v.isCaused === true);
    const newWorkItemDetails = [];
    records.forEach(r => {
        const index = workItemDetails.findIndex(o => o.workItemId === r.id);
        if(index === -1)
            newWorkItemDetails.push({
                id: uuid(),
                usedPartId: claimMaterialDetail.usedPartId,
                usedPartCode: claimMaterialDetail.usedPartCode,
                workItemId: r.id,
                workItemCode: r.code,
                workItemName: r.name,
                laborHour: r.laborHour || 0,
                laborPrice: r.laborPrice || 0,
                laborFee: r.laborHour * r.laborPrice,
                remark: r.remark,
                productCategoryCode: r.productCategoryCode,
                partProductCategoryCodes: claimMaterialDetail.productCategoryCode,
                isCustomed: true
            });
    });
    return dispatch(selectDetailsMore(newWorkItemDetails, 'workItemDetails'));
};
export const CHANGE_CAUSE_MORE = 'CHANGE_CAUSE_MORE';
export const changeCauseMore = (id, value) => ({
    type: CHANGE_CAUSE_MORE,
    id,
    value
});

export const onSubmitMore = () => (dispatch, getState) => {
    const {
        supplierId,
        dutyUnitId,
        details = [],
        accessoryDetails = [],
        secondClaims = [],
        workItemDetails = []
    } = getState()
        .getIn(['page', 'appState', 'submitMoreData'])
        .toJS();
    const detailsData = details.map(item => ({
        usedPartId: item.usedPartId,
        isCaused: item.isCaused,
        quantity: item.quantity
    }));
    const accessoryData = accessoryDetails.map(item => ({
        usedPartId: item.usedPartId,
        quantity: item.quantity
    }));
    const workItemData = workItemDetails.map(item => ({
        usedPartId: item.usedPartId,
        workItemId: item.workItemId,
        isDefault: item.isDefault,
        laborHour: item.laborHour,
        laborPrice: item.laborPrice,
    }));
    return api.updateAllData({
        secondClaims,
        supplierId,
        dutyUnitId,
        details: detailsData,
        accessoryDetails: accessoryData,
        workItemDetails: workItemData
    });
};
